package com.tjhk.kafka.demo;

import com.alibaba.fastjson.JSON;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.IntStream;

/**
 * @ClassName AclProducer
 * @Description 模拟acl producer
 * @Author yang
 * @Date 2020/8/1
 * @Version 1.0
 **/
public class AclProducer {

    private final static Logger LOGGER = LoggerFactory.getLogger(AclProducer.class);

    public static void main(String[] args) {
        KafkaProducer<String, String> producer = new KafkaProducer<>(initProps());
        IntStream.range(0, 10000).forEach(i ->{
            String msg = "测试消息，序号："+i;
            ProducerRecord<String, String> record = new ProducerRecord<>("test-topic", "测试消息"+i, JSON.toJSONString(msg));
            Future<RecordMetadata> future = producer.send(record);
            try {
                RecordMetadata recordMetadata = future.get();
                LOGGER.info("THIS MESSAGE IS SEND DONE AND THE KEY IS {},OFFSET IS {}", i, recordMetadata.offset());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
        producer.flush();
        producer.close();



    }

    private static Properties initProps(){
        final Properties prop = new Properties();
        prop.put("bootstrap.servers","192.168.244.151:9092,192.168.244.152:9092,192.168.244.153:9092");
        prop.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        prop.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        //producer-jaas.properties
        prop.put("security.protocol", "SASL_PLAINTEXT");
        prop.put("sasl.mechanism", "SCRAM-SHA-512");
        prop.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username='test-producer' password='123456';");
        return prop;
    }
}
